package com.ruoyi.shop.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.User;
import com.ruoyi.shop.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * shopController
 *
 * @author ruoyi
 * @date 2025-06-09
 */
@Anonymous
@RestController
@RequestMapping("/shop/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user) {
        User dbUser = userService.selectUserByNumber(user.getUsernumber());
        if (dbUser == null) {
            return AjaxResult.error("用户不存在");
        }

        if (!dbUser.getUserpwd().equals(user.getUserpwd())) {
            return AjaxResult.error("密码错误");
        }

        // 这里可以生成token，若依框架本身有token机制，可以直接使用
        return AjaxResult.success("登录成功", dbUser);
    }
    /**
     * 查询shop列表
     */
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出shop列表
     */

    @Log(title = "shop", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "shop数据");
    }

    /**
     * 获取shop详细信息
     */

    @GetMapping(value = "/{userid}")
    public AjaxResult getInfo(@PathVariable("userid") String userid)
    {
        return AjaxResult.success(userService.selectUserByUserid(userid));
    }

    /**
     * 新增shop
     */

    @Log(title = "shop", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改shop
     */

    @Log(title = "shop", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除shop
     */

    @Log(title = "shop", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userids}")
    public AjaxResult remove(@PathVariable String[] userids)
    {
        return toAjax(userService.deleteUserByUserids(userids));
    }
}
